<?php
namespace App\Http\Controllers\Imessage;

use App\Common\Fun;
use App\Common\Result;
use App\Http\Controllers\Controller;
use App\Service\Customer\CustomerOauthService;
use App\Service\Imessage\wxMessage;
use Illuminate\Http\Request;

class wxTemplate extends Controller
{
    /**
     *  发送模板消息
     */
    public function sendTemplate(Request $request)
    {
        $access_token = wxMessage::getToken();
        if(!isset($access_token['access_token'])){
            Result::msg('-1','error','发送消息错误!');
        }

        $uid = $request->input('uid');
        $user_info = CustomerOauthService::findUser(['uid'=>$uid]);
        if(!$user_info){
            Result::msg('-1','error','用户不存在!');
        }

        $data = array(
            'access_token' => $access_token['access_token'],
            'touser' => $user_info->openid,
            'template_id' => $request->template_id,
            'page' => $request->url,
            'form_id' => $request->form_id,
            'data' => json_decode($request->postJsonString,true)
        );

        $res = Fun::sendUrl('post',"https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$access_token['access_token']}",$data);

        Result::msg(0,'success',$res);
    }
}